package com.party.admin.task;

import com.google.common.collect.Maps;
import com.party.common.utils.DateUtils;
import com.party.core.model.BaseModel;
import com.party.core.model.YesNoStatus;
import com.party.core.model.sign.GroupMember;
import com.party.core.model.sign.SignProject;
import com.party.core.model.sign.SignProjectAuthor;
import com.party.core.service.sign.ISignApplyService;
import com.party.core.service.sign.ISignProjectService;
import com.party.notify.notifyPush.servce.INotifySendService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 通知打卡定时任务
 * Created by yifeng
 *
 * @date 2018/3/5 0005
 * @time 下午 16:04
 */
@Component("quartzNotifySignMember")
public class QuartzNotifySignMember {
    @Autowired
    private ISignProjectService signProjectService;
    @Autowired
    private ISignApplyService signApplyService;
    @Autowired
    private INotifySendService notifySendService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 通知未结束的项目中未打卡的成员打卡
     */
    public void notifySignMember() {
        logger.info("通知未打卡的成员打卡任务开始~");
        SignProjectAuthor project = new SignProjectAuthor();
        project.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        Map<String, Object> params = Maps.newHashMap();
        params.put("notFinished", YesNoStatus.YES.getCode());
        List<SignProjectAuthor> signProjects = signProjectService.projectAuthorList(project, params, null);
        for (SignProject signProject : signProjects) {
            try {
                // 没有打卡的用户id
                List<String> memberIds = signApplyService.getNoSignMember(signProject.getId());
                for (String memberId : memberIds) {
                    try {
                        Integer signCount = signApplyService.getSignMember(signProject.getId(), new Date()).size();
                        notifySendService.notifySign(signProject.getId(), signCount, memberId);
                    } catch (Exception e) {
                        logger.error("发送小程序模板消息异常", e);
                        continue;
                    }
                }
            } catch (Exception e) {
                logger.error("通知未打卡的成员打卡任务异常", e);
                continue;
            }
        }
        logger.info("通知未打卡的成员打卡任务结束~");
    }

    /**
     * 通知昨天的打卡排名
     */
    public void notifySignRank() {
        logger.info("通知昨天的打卡排名任务开始~");
        Date yesterdayDate = DateUtils.addDay(new Date(), -1);
        String yesterday = DateUtils.formatDate(yesterdayDate, DateUtils.DATETIME_PATTERN);
        SignProjectAuthor project = new SignProjectAuthor();
        project.setDelFlag(BaseModel.DEL_FLAG_NORMAL);
        Map<String, Object> resultParam = Maps.newHashMap();
        resultParam.put("gtYesterday", YesNoStatus.YES.getCode());
        resultParam.put("endTime", yesterday);
        List<SignProjectAuthor> signProjects = signProjectService.projectAuthorList(project, resultParam, null);
        for (SignProject signProject : signProjects) {
            try {
                // 已打卡的用户id
                List<String> authorIds = signApplyService.getSignMember(signProject.getId(), yesterdayDate);
                for (String authorId : authorIds) {
                    try {
                        Map<String, Object> params = Maps.newHashMap();
                        params.put("startTime", yesterday);
                        params.put("endTime", yesterday);
                        params.put("memberId", authorId);
                        List<GroupMember> rankListXcx = signApplyService.rankListXcx(signProject.getId(), params, null);
                        if (rankListXcx.size() > 0) {
                            GroupMember groupMember = rankListXcx.get(0);
                            notifySendService.notifySignRank(signProject.getId(), authorId, groupMember.getRank(), groupMember.getStepNum());
                        }
                    } catch (Exception e) {
                        continue;
                    }
                }
            } catch (Exception e) {
                continue;
            }
        }
        logger.info("通知昨天的打卡排名任务结束~");
    }
}
